﻿using System;
using NguyenHoa.Web.App_Code;
using NguyenHoa.Domains;
using NguyenHoa.Services;
using System.IO;
using DevExpress.Web.ASPxDataView;
using System.Web.UI.HtmlControls;

namespace NguyenHoa.Web.Pages
{
  public partial class NewsPage : BasePage
  {
    #region Properties
    private int NewsLimit = 10;
    #endregion

    #region Events
    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsPostBack && !IsCallback)
      {
        LoadNews();
      }

      if (dvNews.IsCallback)
      {
        LoadNews();
      }
    }

    protected void dvNews_DataBound(object sender, EventArgs e)
    {
      foreach (DataViewItem item in dvNews.Items)
      {
        HtmlImage imgPreview = dvNews.FindItemControl("imgPreview", item) as HtmlImage;
        Domains.News news = item.DataItem as Domains.News;
        if (news.ViewCount == null)
        {
          news.ViewCount = 0;
        }

        if (imgPreview != null)
        {
          if (news != null && !string.IsNullOrEmpty(news.ImagePath))
          {
            imgPreview.Src = "~/" + news.ImagePath;
            imgPreview.Style["Display"] = string.Empty;
            imgPreview.Attributes["onclick"] = string.Format("gotoLocalPage('NewsDetailPage.aspx?t=N&id={0}&tid={1}')", news.Id.ToString(), news.CategoryId.ToString());
          }
        }
      }
    }
    #endregion

    #region Methods
    private void LoadNews()
    {
      TList<Domains.News> newsList = new NewsService().GetByHomeSearch(null, CurrentLanguage.Id, TypeId, null, "N");
      dvNews.DataSource = TypeId == 0 ? newsList.GetRange(0, NewsLimit) : newsList;
      dvNews.DataBind();
    }

    public string FormatDate(object value)
    {
      return Utils.FormatDate(value);
    }
    #endregion
  }
}
